const Base = require('./base.js');

module.exports = class extends Base {
  async indexAction() {
    const {page = 1, pagesize = 10} = this.get();
    this.assign('page', page);
    this.assign('pagesize', pagesize);
    return this.display();
  }
  async getListAction(){
    const {page = 1, pagesize = 10,keyword=''} = this.get();
    const time = this.get()['time[]'];
    let where = {}
    if(keyword){
      where.name= ['like',`%${keyword}%`]
    }

    if(time){
      where.updated_at = {
        '>': think.datetime(time[0]),
        '<': think.datetime(time[1])
      }
    }
    const res = await this.model('user_study').where(where).page({page,pagesize}).countSelect()
    return this.success(res)
  }
  async changeStatusAction() {
    if (this.isPost) { // 如果是 POST 请求
      console.log(this.post());
      const {id, status} = this.post().data;
      console.log(id, status);
      const res = await this.model('user_study').where({id}).find()
      if(!think.isEmpty(res)){
        const data = await this.model('user_study').where({id}).update({status:Number(status)})
        return  this.success('修改成功')
      }else {
        return this.fail('对不起数据不存在')
      }
    }else {
      return this.fail('请求方式错误')
    }
  }
};
